#include<iostream>
#include<algorithm>

using namespace std;

typedef long long LL;

const int N=1e5+10;

int n;
int a[N],m[N];


LL k(int a,int b,int mod)
{
	LL sum=1;
	while(b){
		if(b&1) sum=sum*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return sum%mod;
}


int main()
{
	scanf("%d",&n);
	
	LL ans=1;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i],&m[i]);
		ans*=a[i];
	}
	
	LL sum=0;
	for(int i=1;i<=n;i++){
		LL p=ans/a[i];
		sum+=m[i]*p*k(p,a[i]-2,a[i]);
	}
	
	printf("%lld",sum);
	
	return 0;
}
